package view.render.monster
{
	import embeds.EmbedRes;
	
	import enums.main.UISize;
	
	import feathers.controls.Button;
	
	import model.vo.monster.SpVo;
	
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.AtlasTextureGenerator;
	import util.FG2FUGenerator;
	
	import view.component.monster.large_monster.LMVideoListView;
	import view.event.monster.LMonsterVideoListItemEvent;
	import view.render.BaseScrollerItem;
	
	
	/**
	 * 大型怪物-视频收藏列表item
	 * @author £iang
	 * 
	 */	
	public class LMonsterVideoListItem extends BaseScrollerItem
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 星星组 */
		public var starGroup:Sprite;
		/** 灰心图片数组 */
		public var noStarVec:Vector.<Image>;
		/** 星星图片数组 */
		public var starVec:Vector.<Image>;
		/** 编辑按钮 */
		public var editBtn:Button;
		/** 删除按钮 */
		public var deleteBtn:Button;
		/** 取消按钮 */
		public var playBtn:Button;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** item数据 */
		protected var _data:SpVo;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initializeOther():void
		{
			super.initializeOther();
			starGroup = new Sprite();
			scollerContainer.addChild( starGroup );
			
			var i:int;
			var tmpImage:Image;
			var tmpTexture:Texture
			
			noStarVec = new Vector.<Image>;
			starVec = new Vector.<Image>;
			
			for ( i = 0; i < 5; i ++ )
			{
				tmpTexture = Texture.fromBitmapData( new EmbedRes.NOSTAR_ICON().bitmapData );
				tmpImage = new Image( tmpTexture );
				tmpImage.x = i * 46 * UISize.DPI_SCALE;
				tmpImage.scaleX = tmpImage.scaleY = UISize.DPI_SCALE;
				noStarVec.push( tmpImage );
				starGroup.addChild( tmpImage );
				tmpTexture = Texture.fromBitmapData( new EmbedRes.STAR_ICON().bitmapData );
				tmpImage = new Image( tmpTexture );
				tmpImage.x = i * 46 * UISize.DPI_SCALE;
				tmpImage.scaleX = tmpImage.scaleY = UISize.DPI_SCALE;
				starVec.push( tmpImage );
				starGroup.addChild( tmpImage );
			}
			
			var iconVec:Vector.<Texture> = AtlasTextureGenerator.getRandomCatTextures(
				"head", 3, false );
			var tmpIcon:Image;
			
			tmpIcon = new Image( iconVec[ 0 ] );
			tmpIcon.scaleX = tmpIcon.scaleY = UISize.DPI_SCALE;
			editBtn = FG2FUGenerator.getNoBGStyleBtn(
				120, 50, UISize.DPI_SCALE	
			);
			editBtn.label = "编辑";
			editBtn.defaultIcon = tmpIcon;
			editBtn.iconPosition = "top";
			editBtn.gap = 0;
			scollerContainer.addChild( editBtn );
			
			tmpIcon = new Image( iconVec[ 1 ] );
			tmpIcon.scaleX = tmpIcon.scaleY = UISize.DPI_SCALE;
			deleteBtn = FG2FUGenerator.getNoBGStyleBtn(
				120, 50, UISize.DPI_SCALE	
			);
			deleteBtn.label = "删除";
			deleteBtn.defaultIcon = tmpIcon;
			deleteBtn.iconPosition = "top";
			deleteBtn.gap = 0;
			scollerContainer.addChild( deleteBtn );
			
			tmpIcon = new Image( iconVec[ 2 ] );
			tmpIcon.scaleX = tmpIcon.scaleY = UISize.DPI_SCALE;
			playBtn = FG2FUGenerator.getNoBGStyleBtn(
				120, 50, UISize.DPI_SCALE	
			);
			playBtn.label = "观看";
			playBtn.defaultIcon = tmpIcon;
			playBtn.iconPosition = "top";
			playBtn.gap = 0;
			scollerContainer.addChild( playBtn );
			
			addListener();
		}
		
		/**
		 * 调整组件 
		 * 
		 */		
		override protected function draw():void
		{
			var posY:Number = ( _itemH - 43 ) / 2 * UISize.DPI_SCALE;
			var leftPadding:Number = UISize.TITLE_PADDING_LEFT * UISize.DPI_SCALE;
			var firstLabelFullW:Number;
			
			firstLabel.x = leftPadding;
			firstLabelFullW = UISize.MAIN_W * _firstPercent;
			
			starGroup.y = posY;
			starGroup.x = UISize.MAIN_W - firstLabelFullW -
				( 46 * 5 + 20 ) * UISize.DPI_SCALE;
			
			editBtn.y = posY;
			editBtn.x = UISize.MAIN_W * 0.6;
			
			deleteBtn.y = posY;
			deleteBtn.x = UISize.MAIN_W * 0.8;
			
			playBtn.y = posY;
			playBtn.x = UISize.MAIN_W;
			
			scollerContainer.x = firstLabelFullW;
			scollerContainer.height = _itemH * UISize.DPI_SCALE;
			scollerContainer.width = UISize.MAIN_W - firstLabelFullW;
			
			this.width = UISize.MAIN_W;
			this.height = _itemH * UISize.DPI_SCALE;
			
		}
		
		/**
		 * 设置item数据 
		 * @param data 数据
		 * 
		 */		
		public function setData( data:SpVo ):void
		{
			_data = data;
			firstLabel.text = data.name;
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				starVec[ i ].visible = ( i < data.star );
			}
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			editBtn.addEventListener( Event.TRIGGERED, editHandler );
			deleteBtn.addEventListener( Event.TRIGGERED, deleteHandler );
			playBtn.addEventListener( Event.TRIGGERED, playHandler );
		
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理点击编辑按钮后派发的事件 
		 * @param event 点击编辑按钮后派发的事件 
		 * 
		 */		
		protected function editHandler( event:Event ):void
		{
			var videoEvent:LMonsterVideoListItemEvent = new LMonsterVideoListItemEvent( LMonsterVideoListItemEvent.START_EDIT );
			
			videoEvent.data = _data;
			( this.parent.parent.parent.parent as LMVideoListView ).dispatchCommonEvent( videoEvent );
		}
		
		/**
		 * 处理点击删除按钮后派发的事件 
		 * @param event 点击删除按钮后派发的事件 
		 * 
		 */		
		protected function deleteHandler( event:Event ):void
		{
			var videoEvent:LMonsterVideoListItemEvent = new LMonsterVideoListItemEvent( LMonsterVideoListItemEvent.START_DELETE );
			
			videoEvent.data = _data;
			( this.parent.parent.parent.parent as LMVideoListView ).dispatchCommonEvent( videoEvent );
		}
		
		/**
		 * 处理点击观看按钮后派发的事件 
		 * @param event 点击观看按钮后派发的事件 
		 * 
		 */		
		protected function playHandler( event:Event ):void
		{
			var videoEvent:LMonsterVideoListItemEvent = new LMonsterVideoListItemEvent( LMonsterVideoListItemEvent.PLAY );
			
			videoEvent.data = _data;
			( this.parent.parent.parent.parent as LMVideoListView ).dispatchCommonEvent( videoEvent );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override protected function disposeAnythingInContainer():void
		{
			editBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( editBtn );
			editBtn = null;
			
			deleteBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( deleteBtn );
			deleteBtn = null;
			
			playBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( playBtn );
			playBtn = null;
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				( noStarVec[ i ] as Image ).dispose();
				( noStarVec[ i ] as Image ).texture.dispose();
				noStarVec[ i ] = null;
				( starVec[ i ] as Image ).dispose();
				( starVec[ i ] as Image ).texture.dispose();
				starVec[ i ] = null;
			} 
			
			noStarVec.length = 0;
			noStarVec = null;
			starVec.length = 0;
			starVec = null;
			
			starGroup.dispose();
			starGroup = null;
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function LMonsterVideoListItem()
		{
			super();
			_itemH = UISize.LMONSTER_VIDEO_ITEM_H;
			_firstPercent = 0.4;
			initialize();
		}
	}
}